﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Interface;
using LY.Entity;
using LY.Entity.PageSearch;
using NUnit.Framework;


namespace QJ.CMS.Controllers
{
    public class ArticleController : Controller
    {

        private IArticleService _article;
        private IMenuService _menu;

        public ArticleController(IArticleService articleService, IMenuService menuService)
        {
            _article = articleService;
            _menu = menuService;
        }

        //
        // GET: /Article/ 
        public ActionResult List(string category)
        {
            return View();
        }

        public ActionResult Detail(int id)
        {
            try
            {
                var model = _article.GetArticle(id);

                ViewData.Model = model;
            }
            catch (Exception ex)
            {
                TempData.Add("msg", ex.Message);
                return RedirectToAction("Error", "Default");
            }
            return View();
        }

        public ActionResult NewsList()
        {


            int id = 0;
            int menuid = 0;
            int mainmenuid = 0;

            //C_Article Article = new C_Article();
            //List<C_Article> ArticleList = new List<C_Article>();

            ViewBag.ArticleList = new List<C_Article>();
            ViewBag.Article = new C_Article();

            ViewBag.CurrentMenu = new C_Menu();
            ViewBag.MainMenu = new C_Menu();
            ViewBag.LeftMenu = new List<C_Menu>();

            if (Request.QueryString["id"] != null)
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
                ViewBag.Article = _article.GetArticle(id);
                if (ViewBag.Article != null)
                {
                    ViewBag.ArticleList.Add(ViewBag.Article);
                    C_Menu menu = _menu.GetMenu(ViewBag.Article.MenuId);
                    ViewBag.CurrentMenu = menu;
                    C_Menu mainmenu = _menu.GetMenu(menu.UpperID);
                    ViewBag.MainMenu = mainmenu;
                    ViewBag.LeftMenu = _menu.QueryMainMenuListByMainMenuId(mainmenu.ID);
                }

            }
            else
            {
                if (Request.QueryString["menuid"] != null)
                {
                    menuid = Convert.ToInt32(Request.QueryString["menuid"]);
                    ViewBag.ArticleList = _article.GetArticleList(menuid, 10);
                    C_Menu menu = _menu.GetMenu(menuid);
                    ViewBag.CurrentMenu = menu;
                    C_Menu mainmenu = _menu.GetMenu(menu.UpperID);
                    ViewBag.MainMenu = mainmenu;
                    ViewBag.LeftMenu = _menu.QueryMainMenuListByMainMenuId(mainmenu.ID);
                }
                else
                {
                    if (Request.QueryString["mainmenuid"] != null)
                    {
                        mainmenuid = Convert.ToInt32(Request.QueryString["mainmenuid"]);
                        ViewBag.MainMenu = _menu.GetMenu(mainmenuid);
                        ViewBag.LeftMenu = _menu.QueryMainMenuListByMainMenuId(ViewBag.MainMenu.ID);
                        if (ViewBag.LeftMenu.Count > 0)
                        {
                            C_Menu menu = _menu.GetMenu(ViewBag.LeftMenu[0].ID);
                            ViewBag.CurrentMenu = menu;
                            ViewBag.ArticleList = _article.GetArticleList(menu.ID, 10);
                        }
                    }
                }
            }
            ViewBag.ArticleCount = ViewBag.ArticleList.Count;

            return View();
        }

    }
}
